myApp.controller('advertListController',function($scope,toaster,$uibModal,$filter,$state,$log,$serAdvert,FileUploader,$serImgUpload){
    var ctrl = this;
    ctrl.vm = {
        data:{},
        search:{
            id:'',
            name:'',
            status:'',
        },
        searchFn:function(){
            vm.init();
        },
        reset:function(){
            ctrl.vm.search.id='';
            ctrl.vm.search.name='';
            ctrl.vm.search.status='';
            vm.init();
        },
    };

    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };
    $scope.model = {
        open: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'addAdvert.html',
                controller: 'addAdvertCtrl',
                windowClass: 'AdvertOpen',
                size: 'lg',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        open1: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'upAdvert.html',
                controller: 'upAdvertCtrl',
                windowClass: 'AdvertOpen',
                size: 'lg',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        del:function (data) {
            var modalInstance = $uibModal.open({
                template: '<div class="modal-header"><h3 class="modal-title">删除确认</h3></div>' +
                '<div class="modal-footer"><button class="btn btn-default" ng-click="cancel()">Cancel</button>' +
                '<button class="btn btn-primary" ng-click="ok()">OK</button></div>',
                controller: 'deladvertController',
                windowClass: 'advertList',
                size: 'sm',
            });
            modalInstance.result.then(function (res) {
                var type='';
                if(res){
                    $serAdvert.delIt({
                        id:data.id
                    }).then(function(res){
                        switch (res.code){
                            case 1:type = 'success';break;
                            default:type = 'error';break;
                        }
                        toaster.pop(type, '信息', res.message);
                        vm.init();
                    })
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    var vm ={
        init:function(page){
            $serAdvert.getList({
                //type:1,
                page:page||1,
                limit:$scope.placement.selected,
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                }else{
                    ctrl.vm.data ="";
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
    myApp.controller('addAdvertCtrl', function($scope, $uibModalInstance, info, $serAdvert, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            position:'',
            value:'',
            type:'',
            button_word:'',
            word_2:'',
            word_1:'',
        }
        //图片上传
        var uploader = $scope.uploader = $serImgUpload.setImgUpload('advert_',true);
        $serImgUpload.putImgUpload(uploader,'advert',function(){
            $serImgUpload.upload(uploader);
        });
        uploader.onSuccessItem = function(fileItem) {
            $scope.fileItem = fileItem.file.name;
        };
        //图片上传end

        $scope.ok = function () {
            $serAdvert.addIt({
                title:$scope.vm.title,
                image:$scope.fileItem,
                status:$scope.vm.status,
                position:$scope.vm.position,
                value:$scope.vm.value,
                type:$scope.vm.type,
                button_word:$scope.vm.button_word,
                word_2:$scope.vm.word_2,
                word_1:$scope.vm.word_1,

            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });

    myApp.controller('upAdvertCtrl', function($scope, $uibModalInstance, info,$serAdvert, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            position:'',
            value:'',
            type:'',
            button_word:'',
            word_2:'',
            word_1:'',
        }


        function init(){
            $scope.fileItem = info.image
            $scope.vm.status = info.status
            $scope.vm.position = info.position
            $scope.vm.title = info.title
            $scope.vm.value = info.value
            $scope.vm.type = info.type
            $scope.vm.button_word = info.button_word
            $scope.vm.word_2 = info.word_2
            $scope.vm.word_1 = info.word_1
        };
        init();

        //图片上传
        var uploader = $scope.uploader = $serImgUpload.setImgUpload('advert_',true);

        $scope.clearItems = function () {
            $serImgUpload.putImgUpload(uploader,'advert',function(){
                $serImgUpload.upload(uploader);

            });
            uploader.onSuccessItem = function(fileItem) {
                $scope.fileItem = fileItem.file.name;
            };
        }


        $scope.ok = function () {
            $serAdvert.updateIt({
                id:info.id,
                title:$scope.vm.title,
                image:$scope.fileItem,
                status:$scope.vm.status,
                position:$scope.vm.position,
                value:$scope.vm.value,
                type:$scope.vm.type,
                button_word:$scope.vm.button_word,
                word_2:$scope.vm.word_2,
                word_1:$scope.vm.word_1,
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });

});
myApp.controller('deladvertController', ['$scope', '$uibModalInstance', function($scope,$uibModalInstance) {
    $scope.ok = function () {
        $uibModalInstance.close(true);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);


